<?php

require_once 'Zend/Db/Table/Abstract.php';

class cache extends Zend_Db_Table
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;

    protected function _setup()
    {
        $this->_name = 'cache';
        $this->_primary = 'Id';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    // Function to get the info in the BDD
    public function getInfo($idClient, $serviceResp)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('Session = ?', $idClient);
        $this->_select->where('Appel = ?', $serviceResp);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        else
            return false;
    }

    // Function to write in the BDD
    public function writeInfo($idClient, $serviceResp, $body)
    {
        $val = array('Session' => $idClient, // Id > session_id()
            'Appel' => $serviceResp, // Pointeur de Fonction
            'Xml' => $body);       // Body sous forme d' XML
        $this->_db->insert($this->_name, $val);
        return true;
    }

    // Function to delete info
    public function deleteInfo($idClient, $serviceResp, $id)
    {
        $arr = array('Id = ?' => $id,
            'Session = ?' => $idClient,
            'Appel = ?' => $serviceResp);
        $this->_db->delete($this->_name, $arr);
        return true;
    }

    public function deleteAllInfoSessionService($idClient, $serviceResp)
    {
        $arr = array('Session = ?' => $idClient,
            'Appel = ?' => $serviceResp);
        $this->_db->delete($this->_name, $arr);
        return true;
    }

    public function deleteAllInfoSession($idClient)
    {
        $arr = array('Session = ?' => $idClient);
        $this->_db->delete($this->_name, $arr);
        return true;
    }

    public function getSize($idClient)
    {
        $this->_select = $this->_db->query('SELECT COUNT(*) FROM ' . $this->_name);
        $a = $this->_select->fetchAll();
        $result[0] = $a[0]["COUNT(*)"];
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('Session = ?', $idClient);
        $this->_select = $this->_db->query($this->_select);
        $result[1] = $this->_select->fetchAll();
        return $result;
    }

    public function deleteAllInfo()
    {
        $this->_db->query('TRUNCATE TABLE ' . $this->_name);
        return true;
    }

}
?>